﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Nirvana
{

    /// <summary>
    /// 对图片进行左右/上下翻转，避免脚本修改sacle来设置翻转问题
    /// </summary>
    [AddComponentMenu("Nirvana/UI/Effects/Flippable")]
    public class UIFlippable : BaseMeshEffect
    {
        //左右翻转
        [SerializeField]
        private bool horizontal;
        //上下翻转
        [SerializeField]
        private bool veritical;

        public override void ModifyMesh(VertexHelper vh)
        {
            RectTransform rectTransform = transform as RectTransform;
            for(int i = 0; i < vh.currentVertCount; i++)
            {
                UIVertex uivertex = default(UIVertex);
                vh.PopulateUIVertex(ref uivertex, i);
                uivertex.position = new Vector3((!this.horizontal) ? uivertex.position.x
                    : (uivertex.position.x + (rectTransform.rect.center.x - uivertex.position.x) * 2f),
                    (!this.veritical) ? uivertex.position.y
                    : (uivertex.position.y + (rectTransform.rect.center.y - uivertex.position.y) * 2f),
                    uivertex.position.z);

                vh.SetUIVertex(uivertex, i);
            }
        }

#if UNITY_EDITOR
        protected override void OnValidate()
        {
            if(base.graphic != null)
            {
                base.graphic.SetVerticesDirty();
            }

        }
#endif

    }
}

